/*
 * CallPhoneDialog.java
 * business
 *
 * Created by ChenTao on 2017/1/18.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yidiandao.business.R;
import com.yidiandao.business.utils.UIUtils;

/** 首页拨号 */
public class CallPhoneDialog extends Dialog implements View.OnClickListener {

    private String mNum;

    public CallPhoneDialog(Context context) {
        super(context, R.style.BottomDialogTheme);

        setContentView(R.layout.dialog_call_phone);
        initWindowStyle();
        initView();
    }

    private void initWindowStyle() {
        // 改变样式-->window的样式
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            // 设置弹出位置
            params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;

            window.setAttributes(params);
        }
    }

    private void initView() {

        findViewById(R.id.tv_call).setOnClickListener(this);
        findViewById(R.id.tv_copy).setOnClickListener(this);
        findViewById(R.id.tv_cancle).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_call:
                Uri uri = Uri.parse("tel:" + mNum);
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                UIUtils.getContext().startActivity(intent);
                break;
            case R.id.tv_copy:
                // 得到剪贴板管理器
                ClipboardManager cmb =
                        (ClipboardManager)
                                UIUtils.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(mNum);
                UIUtils.showToast("复制成功");
                break;
        }
        dismiss();
    }

    public void setPhoneNum(String num) {
        mNum = num;
    }
}
